let RegionCode=null;
try {
  RegionCode=import('/@/common/region_code')
} catch (e) {
  RegionCode=null;
}

if(RegionCode===null){
  try {
    RegionCode=import('../region_code')
  } catch (e) {
    
  }
}

/**
 * 获取url中的参数
 * @param variable 如果传递表示获取指定url中的值
 */
export const getQueryParams = (variable: string = '') => {
  var position = window.location.href.indexOf('?');
  if (position < 0) {
    return '';
  }
  var query = window.location.href.substring(position + 1);
  var vars = query.split('&');
  var result = {};

  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    result[pair[0]] = pair[1];
  }

  if (variable) {
    return result[variable] === undefined ? '' : result[variable];
  }
  return result;
};

/**
 * 获取表单的值，有些需要要求是数组的，用于根据表单项的类型来规范默认值的类型，一般是用于校正数组
 * @param data 表单项或者表单项的类型，如Input、Upload,如果是实例化的Form，传递的时候需要调用Form.params
 * @param value 值
 */
export const getFormDefaultValue=(data:string|object,value:any)=>{
  
  let type=typeof data;
  let component='';
  // 表单项已经设置的默认值
  let defaultValue='';
  if(type==="string"){
    component=data;
  }else if (type==="object"){
    component=data?.component;
    defaultValue=data?.defaultValue;
  }
  
  if (value===undefined || value==='undefined' || value===null || value==="null") {
      // 当取值异常的时候，则使用默认值
      value = defaultValue;
  }
  
  const isEmpty=(value)=>{
    if(typeof value===undefined || value===null || value=="null"){
      return true;
    }
    return false;
  }
  
  const toArray = (value) => {
    // 是数组直接返回
    if(Array.isArray(value)){
      return value;
    }
    // 为空则直接返回数组
    if(isEmpty(value)){
      return [];
    }
    // 不为空且不是数组，则生成一个数组
    if (!Array.isArray(value)) {
      return [value];
    }
    return value;
  };
  
  switch (component) {
    case 'Upload':
    case 'Checkbox':
    case 'Cascader':
      value=toArray(value);
      break;
    case 'RadioGroup':
      value = parseInt(value);
      if(isNaN(value)){
        value=0;
      }
      break;
  }
  //console.log('item.field',data,Array.isArray(value),value);
  
  // 这里要注意0值
  return !isEmpty(value)? value : '';
}

export const resetFormValue=(fields:Array<object>,data:object={})=>{
  for (let item of fields) {
    const field=item?.field;
    if(!field){
      continue;
    }
    fields[field] = getFormDefaultValue(item.component,data[field]);
  }
  return fields;
}

export const transRegionByArray = (data:Array<number>) => {
  let value = [];
  let province_id = !data[0] ? '' : data[0];
  let city_id = !data[1] ? '' : data[1];
  let area_id = !data[2] ? '' : data[2];
  if (province_id && RegionCode[province_id]) {
    value.push(RegionCode[province_id]);
  }
  if (city_id && RegionCode[city_id]) {
    value.push(RegionCode[city_id]);
  }
  if (area_id && RegionCode[area_id]) {
    value.push(RegionCode[area_id]);
  }
  return value;
};

export const transRegionByObject = data => {
  let value = [];
  let province_id = data?.province_id;
  let city_id = data?.city_id;
  let area_id = data?.area_id;
  if (province_id && RegionCode[province_id]) {
    value.push(RegionCode[province_id]);
  }
  if (city_id && RegionCode[city_id]) {
    value.push(RegionCode[city_id]);
  }
  if (area_id && RegionCode[area_id]) {
    value.push(RegionCode[area_id]);
  }
  return value;
};

/**
 * @param data 可以传递['省id','市id','县id'] 或者 {province_id:xxx,city_id:xxx,area_id:xxxx}
 */
export const transRegion = (data: any) => {
  let value = [];
  if (Array.isArray(data)) {
    value = transRegionByArray(data);
  } else {
    value = transRegionByObject(data);
  }
  return value.join(',');
};
